﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ProductionManage.Models.DataModel;
using ProductionManage.Models.Products;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using test.demo.Model.WM仓储管理;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using wangyaxin.Wang.Dto;
using wangyaxin.Zhangduo.MdItemType;
using wangyaxin.Zhangduo.MdItemType.Dto;
using wangyaxin.Zhangduo.WMItemRecpt;
using wangyaxin.Zhangduo.WMItemRecpt.Dto;
using wangyaxin.Zhangduo.WorkShops.Dto;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace wangyaxin.Zhangduo
{
    [ApiExplorerSettings(GroupName = "张铎")]
    public class WMItemRecptService : ApplicationService, IWMItemRecptAppService
    {
        public readonly IRepository<WM_Item_Recpt,int> _repository;
        public readonly IRepository<WM_Warehouse, int> _Warehouserepository;
        public readonly IRepository<Vendor, int> _Vendorrepository;
        public WMItemRecptService(IRepository<WM_Item_Recpt, int> repository, IRepository<WM_Warehouse, int> warehouserepository, IRepository<Vendor, int> vendorrepository)
        {
            _repository = repository;
            _Warehouserepository = warehouserepository;
            _Vendorrepository = vendorrepository;
        }
        /// <summary>
        /// 显示采购入库
        /// </summary>
        /// <param name="RecptCode">入库单编号</param>
        /// <param name="RecptName">入库单名称</param>
        /// <param name="VendorName">供应商名称</param>
        /// <param name="PoCode">订单编号</param>
        /// <param name="RecptDate">入库日期</param>
        /// <returns>返回满足条件的入库列表</returns>
        [HttpGet, Route("GetWMitemRecpt")]
        public async Task<string> GetWMitemRecpt(string? RecptCode, string? RecptName, string? VendorName, string? PoCode, string? RecptDate)
        {
            var item = await _repository.GetListAsync();
            if (!string.IsNullOrEmpty(RecptCode))
            {
                item = item.Where(c => c.RecptCode.Contains(RecptCode)).ToList();
            }
            if (!string.IsNullOrEmpty(RecptName))
            {
                item = item.Where(c => c.RecptName.Contains(RecptName)).ToList();
            }
            if (!string.IsNullOrEmpty(VendorName))
            {
                item = item.Where(c => c.VendorName.Contains(VendorName)).ToList();
            }
            if (!string.IsNullOrEmpty(PoCode))
            {
                item = item.Where(c => c.PoCode.Contains(PoCode)).ToList();
            }
            if (!string.IsNullOrEmpty(RecptDate))
            {
                var date = Convert.ToDateTime(RecptDate);
                item = item.Where(c => c.RecptDate == date).ToList();
            }
            var itemList = ObjectMapper.Map<List<WM_Item_Recpt>, List<WMItemRecptDto>>(item);
            return JsonConvert.SerializeObject(new PageDto<WMItemRecptDto> { code = "", data = itemList, msg = "", count = itemList.Count });
        }

        /// <summary>
        /// 显示仓库
        /// </summary>
        /// <returns>返回仓库列表</returns>
        [HttpGet, Route("GetWmWarehouse")]
        public async Task<string> GetWmWarehouse()
        {
            var item=await _Warehouserepository.GetListAsync();
            var itemList = ObjectMapper.Map<List<WM_Warehouse>, List<WM_Warehouse>>(item);
            return JsonConvert.SerializeObject(new PageDto<WM_Warehouse> { code = "", data = itemList, msg = "", count = itemList.Count });
        }

        /// <summary>
        /// 显示供应商
        /// </summary>
        /// <returns>返回供应商列表</returns>
        [HttpGet, Route("GetVendor")]
        public async Task<string> GetVendor()
        {
            var item = await _Vendorrepository.GetListAsync();
            var itemList = ObjectMapper.Map<List<Vendor>, List<Vendor>>(item);
            return JsonConvert.SerializeObject(new PageDto<Vendor> { code = "", data = itemList, msg = "", count = itemList.Count });
        }

        /// <summary>
        /// 添加采购信息
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost, Route("AddWMitemRecpt")]
        public async Task<string> AddWMitemRecpt(WM_Item_Recpt model)
        {
            var itemRecpt = new WM_Item_Recpt
            {
                RecptCode = model.RecptCode,
                RecptName = model.RecptName,
                IqcId = model.IqcId,
                IqcCode = model.IqcCode,
                PoCode = model.PoCode,
                VendorId = model.VendorId,
                VendorCode = model.VendorCode,
                VendorName = model.VendorName,
                VendorNick = model.VendorNick,
                WarehouseId = model.WarehouseId,
                WarehouseCode = model.WarehouseCode,
                WarehouseName = model.WarehouseName,
                LocationId = model.LocationId,
                LocationCode = model.LocationCode,
                LocationName = model.LocationName,
                AreaId = model.AreaId,
                AreaCode = model.AreaCode,
                AreaName = model.AreaName,
                RecptDate = model.RecptDate,
                Status = model.Status,
                Remark = model.Remark,
                CreateBy = "张三",
                CreateTime = DateTime.Now
            };
            await _repository.InsertAsync(itemRecpt);
            var Dto = ObjectMapper.Map<WM_Item_Recpt, WMItemRecptDto>(itemRecpt);
            return JsonConvert.SerializeObject(new PageDto<WMItemRecptDto>
            {
                code = "", // 可以根据需要设置状态码
                data = new List<WMItemRecptDto> { Dto }, // 返回添加的用户信息
                msg = "添加成功", // 可以根据需要添加消息
                count = 1 // 添加的用户数量
            });
        }

    }
}
